﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace SuperPongControl
{
    public class ConnectionManager
    {
        private TcpListener tcpListener;
        private Thread listenThread;


        private int numPlayers =0;

        public ConnectionManager()
        {
          
        }

        public void startServer()
        {
            System.Console.WriteLine("Starting listening ");
            tcpListener = new TcpListener(IPAddress.Any, 6668);

            listenThread = new Thread(new ThreadStart(ListenForClients));
            listenThread.Start();

        }

       

        private void ListenForClients()
        {
            this.tcpListener.Start();


            // while num of cilients < 2
            while (numPlayers < 2)
            {
              
                TcpClient client = this.tcpListener.AcceptTcpClient();
                System.Console.WriteLine("New client " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                Tuple<TcpClient, int> param = new Tuple<TcpClient, int>(client, numPlayers);
                numPlayers++;
                clientThread.Start(param);
            }
        }

        private void HandleClientComm(object p)
        {

            Tuple<TcpClient, int> param = (Tuple<TcpClient, int>) p ;
            int playerNumber = param.Item2;
            TcpClient tcpClient = (TcpClient)param.Item1;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {                
                    break;
                }

                if (bytesRead == 0)
                {
                
                    break;
                }
              
                ASCIIEncoding encoder = new ASCIIEncoding();
                System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));

                if (encoder.GetString(message, 0, bytesRead).Contains("LEFT")){
                    Tuple<string, int> move = new Tuple<string, int>("LEFT" , playerNumber);
                    InputHandler.movments.Enqueue(move);

                }
                else if (encoder.GetString(message, 0, bytesRead).Contains("RIGHT")){
                     Tuple<string, int> move = new Tuple<string, int>("RIGHT" , playerNumber);
                    InputHandler.movments.Enqueue(move);
                }

            }

            tcpClient.Close();
        }



    }
}
